// ~ CopyRight © 2012 USTC SINOVATE  SOFTWARE CO.LTD All Rights Reserved.
package com.cmcc.zysoft.infocollection.mobile.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author yan.dou
 * @date 2012-11-18 下午3:04:27
 */
@Repository
public class SystemUserDao extends HibernateBaseDaoImpl<SystemUser, String> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * @param employeeId
	 * @param userCode
	 * @param pass
	 * @return
	 */
	public Map<String,Object> checkLogin(String employeeId,String userCode, String pass) {
		String sql = "";
		if(employeeId == null || "".equals(employeeId)){
			 sql = "select u.user_name as userCode,u.user_name as userName,'' as employeeId,'' as departmentId,'' as departmentName " +
			 		" from tb_c_system_user u" +
					" where u.user_Name = ? and" +
					" u.password = ? and " +
					" u.del_flag = '0'";
		}else{
			 sql = "select u.user_name as userCode,e.employee_name as userName,e.employee_id as employeeId," +
			 		" dept.department_id as departmentId,dept.department_name as departmentName " +
			 		" from tb_c_system_user u,tb_c_employee e,tb_c_department dept" +
					" where u.employee_id = e.employee_id and e.department_id = dept.department_id" +
					" and u.user_Name = ?" +
					" and u.password = ?" +
					" and u.del_flag = '0'";
		}	
		List<Map<String,Object>> list = this.jdbcTemplate.queryForList(sql,userCode,pass);
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public SystemUser getEmpIdFromSys(String userCode,String pass){
		String hql = "from SystemUser sys where sys.userName = ? and sys.password = ?";
		List<SystemUser> list = this.findByHQL(hql,userCode,pass);
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	/**
	 * 根据用户userid 获取公司id;
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SystemUser getCompanyIdByUserId(String userId) {
		String hql = "from SystemUser sys where sys.userName = ?";
		List<SystemUser> list = this.findByHQL(hql, userId);
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 根据员工id获取公司id
	 * @param empId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SystemUser getCompanyIdByEmpId(String empId) {
		String hql = "from SystemUser sys where sys.employeeId = ?";
		List<SystemUser> list = this.findByHQL(hql, empId);
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * @param userCode
	 * @return
	 * writer by wmliu
	 */
	@SuppressWarnings("unchecked")
	public SystemUser getSystemByuserCode(String userCode) {
		String hql = "from SystemUser sys where sys.delFlag ='0' and sys.userName = ? ";
		List<SystemUser> list = this.findByHQL(hql,userCode);
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}

	/**
	 * 获取登录信息
	 * @param systemUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getLoginMessager(SystemUser systemUser) {
		String employeeId = systemUser.getEmployeeId();
		if(StringUtils.hasText(employeeId)){
			String hql = "select new Map(" +
					" user.userName as userCode," +
					" user.employeeId as employeeId," +
					" emp.employeeName as userName," +
					" dept.departmentId as departmentId," +
					" dept.departmentName as departmentName" +
					" ) from SystemUser user,Employee emp,Department dept " +
					" where user.userId = ? and user.employeeId = emp.employeeId and emp.departmentId = dept.departmentId";
			return this.findByHQL(hql, systemUser.getUserId());
		}else{
			String hql = "select new Map(" +
					" user.userName as userCode," +
					" '' as userName," +
					" '' as employeeId," +
					" '' as departmentId," +
					" '' as departmentName" +
					" ) from SystemUser user where user.userId = ? ";
			return this.findByHQL(hql, systemUser.getUserId());
		}
	}

	
	
	
	
	
	
	
	//================================================== 以下方法为新增方法 ==================================================
	
}